package io.github.doc.ui;

import cn.hutool.core.collection.CollectionUtil;
import com.intellij.openapi.options.Configurable;
import com.intellij.util.ui.FormBuilder;
import io.github.doc.common.ConfigKey;
import io.github.doc.config.PluginSetting;
import io.github.doc.config.state.PluginSettingsState;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * <p>Company: 深圳振瀚信息技术有限公司成都分公司 </p>
 * <p>Description: </p>
 *
 * @author ZH.Team
 * @version 1.0.0
 * @email "mailto:ZH.Team@zhxx.com"
 * @date 2024.12.01 20:23
 * @since y.y.y
 */
public class PluginSettingsConfigurable implements Configurable {


    /**
     * 主UI界面
     */
    private JPanel mainPanel;

    /**
     * 输入框组件
     */
    private List<Wrapper> baseComponents = new LinkedList<>();

    /**
     * Template components
     */
    private List<Wrapper> templateComponents = new LinkedList<>();

    /**
     * Plugin settings configurable
     *
     * @since y.y.y
     */
    public PluginSettingsConfigurable() {
        this.baseComponents.add(new TextComponentWrapper("作者", ConfigKey.AUTHOR, ConfigKey.AUTHOR_DEFAULT));
        this.baseComponents.add(new TextComponentWrapper("邮箱", ConfigKey.EMAIL, ConfigKey.EMAIL_DEFAULT));
        this.baseComponents.add(new TextComponentWrapper("版本号", ConfigKey.VERSION, ConfigKey.VERSION_DEFAULT));

        this.templateComponents.add(new TextAreaComponentWrapper("类注释", ConfigKey.CLASS_COMMENT, 5, 5));
        this.templateComponents.add(new TextAreaComponentWrapper("方法注释", ConfigKey.METHOD_COMMENT, 5, 5));
        this.templateComponents.add(new TextAreaComponentWrapper("字段注释", ConfigKey.FILED_COMMENT, 5, 5));
    }


    /**
     * Gets display name *
     *
     * @return the display name
     * @since y.y.y
     */
    @Override
    @Nls(capitalization = Nls.Capitalization.Title)
    public String getDisplayName() {
        return "Easy Doc";
    }

    /**
     * Create component
     *
     * @return the j component
     * @since y.y.y
     */
    @Override
    public @Nullable JComponent createComponent() {
        FormBuilder formBuilder = FormBuilder.createFormBuilder();
        PluginSettingsState state = PluginSettingsState.getInstance();
        if (CollectionUtil.isNotEmpty(this.baseComponents)) {
            formBuilder.addLabeledComponent(new JLabel("基础配置信息"), new JSeparator(), 1, false);
            this.baseComponents.forEach(component -> {
                component.load(state.getState());
                component.builderComponent(formBuilder);
            });
        }

        if (CollectionUtil.isNotEmpty(this.templateComponents)) {
            int topInset = CollectionUtil.isNotEmpty(this.baseComponents) ? 10 : 1;
            formBuilder.addLabeledComponent(new JLabel("模板配置"), new JSeparator(), topInset, false);
            this.templateComponents.forEach(component -> {
                component.load(state.getState());
                component.builderComponent(formBuilder);
            });
        }
        formBuilder.addComponentFillVertically(new JPanel(), 1);
        this.mainPanel = formBuilder.getPanel();
        return this.mainPanel;
    }

    /**
     * Is modified
     *
     * @return the boolean
     * @since y.y.y
     */
    @Override
    public boolean isModified() {
        boolean isModified = false;
        if (CollectionUtil.isNotEmpty(this.baseComponents)) {
            for (Wrapper component : this.baseComponents) {
                isModified = this.modified(component);
                if (isModified) {
                    return isModified;
                }
            }
        }

        if (CollectionUtil.isNotEmpty(this.templateComponents)) {
            for (Wrapper component : this.templateComponents) {
                isModified = this.modified(component);
                if (isModified) {
                    return isModified;
                }
            }
        }
        return isModified;
    }

    /**
     * Modified
     *
     * @param component component
     * @return the boolean
     * @since y.y.y
     */
    private boolean modified(Wrapper component) {
        //先获取到输入的值
        Object inputValue = component.getValue();
        PluginSettingsState state = PluginSettingsState.getInstance();
        PluginSetting pluginSetting = state.getState();
        if (pluginSetting == null) {
            return !Objects.equals(inputValue, component.getDefaultValue());
        }
        Object configValue = pluginSetting.getConfig(component.getKey());
        if (configValue == null) {
            return !Objects.equals(inputValue, component.getDefaultValue());
        }
        return !Objects.equals(configValue, inputValue);
    }

    /**
     * 保存
     *
     * @since y.y.y
     */
    @Override
    public void apply() {
        if (CollectionUtil.isNotEmpty(this.baseComponents)) {
            this.baseComponents.forEach(Wrapper::save);
        }

        if (CollectionUtil.isNotEmpty(this.templateComponents)) {
            this.templateComponents.forEach(Wrapper::save);
        }
    }

    /**
     * Reset
     *
     * @since y.y.y
     */
    @Override
    public void reset() {
        if (CollectionUtil.isNotEmpty(this.baseComponents)) {
            this.baseComponents.forEach(Wrapper::reset);
        }

        if (CollectionUtil.isNotEmpty(this.templateComponents)) {
            this.templateComponents.forEach(Wrapper::reset);
        }
    }

    /**
     * Dispose ui resources
     *
     * @since y.y.y
     */
    @Override
    public void disposeUIResources() {
        this.mainPanel = null;
        this.baseComponents = null;
        this.templateComponents = null;
    }
}
